# -*- coding: utf-8 -*-
"""
Created on Mon Oct 24 21:49:24 2022

@author: Administrator
"""

import os

from xml.dom.minidom import parse
import xml.dom.minidom
import uuid

DOMTree = xml.dom.minidom.parse('ptsd_03.xml')

collection = DOMTree.documentElement

nodes = collection.getElementsByTagName('node')

chatNodeFile = 'chatNodeFile.sql'
chatOptionFile = 'ChatOptionFile.sql'
chatNodeChatOption = "chatNodeChatOption.sql"

for filePath in [chatNodeFile, chatOptionFile,chatNodeChatOption]:
    file = open(filePath, 'w')
    file.truncate()
    file.close()


for node in nodes:
    if node.hasAttribute('id'):
        print('id: %s' % node.getAttribute('id'))

        id =  node.getAttribute('id')
        describe = node.getAttribute('describe')
        say = node.getAttribute('say')
        nextNodeID = ''
        nextNode = node.getElementsByTagName('next')
        audio = node.getAttribute('audio')
        audioName = node.getAttribute('audioName')

        if len(nextNode) > 0:
            nextNodeID =nextNode[0].getAttribute('node')

        chatNodeFileObject = open(chatNodeFile, 'a')
        ChatOptionFileObject = open(chatOptionFile, 'a')
        sql = "insert into Chat_Node(id, description, say, audio, audio_name , next_nodeid) values('" + id + "',"
        if describe == '':
            sql = sql + ' null, '
        else:
            sql = sql +" '" + describe + "'" + ', '

        if say == '':
            sql = sql + 'null, '
        else:
            sql = sql + "'" + say + "'" + ', '

        if audio == '':
            sql = sql + 'null, '
        else:
            sql = sql + "'" + audio + "'" + ', '

        if audioName == '':
            sql = sql + 'null, '
        else:
            sql = sql + "'" + audioName + "'" + ', '

        if nextNodeID == '':
            sql = sql + 'null);\n'
        else:
            sql = sql + "'" + nextNodeID + "'" + ");\n"

        chatNodeFileObject.write(sql)

        userNodes = node.getElementsByTagName('userNode')
        optionType = ''
        if len(userNodes) > 0:
            userNode = userNodes[0]
            optionType = userNode.getAttribute('type')
            options = userNode.getElementsByTagName('option')
            for option in options:
                optionVal = option.getAttribute('value')
                optionNext = option.getAttribute('next')
                optionContent = option.firstChild.nodeValue

                optionSql = 'insert into Chat_Option(refid, value, next, content) values('
                uid =str(uuid.uuid1())
                optionSql = optionSql + "'" + uid + "', "
                optionSql = optionSql + "'" + optionVal + "', "
                optionSql = optionSql + "'" + optionNext + "', "
                optionSql = optionSql + "'" + optionContent + "');\n"

                ChatOptionFileObject = open(chatOptionFile, 'a')
                ChatOptionFileObject.write(optionSql)
                chatNodeFileObject.close()

                chatNodeChatOptionSql = 'insert into chat_node_chat_options(chat_node_id, chat_options_refid) values(' + "'"
                chatNodeChatOptionSql = chatNodeChatOptionSql + id + "'," + "'"
                chatNodeChatOptionSql = chatNodeChatOptionSql + uid + "');\n"

                chatNodeChatOptionObj = open(chatNodeChatOption, 'a')
                chatNodeChatOptionObj.write(chatNodeChatOptionSql)
                chatNodeChatOptionObj.close()



